package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.Result;
import com.luo.model.User;
import com.luo.service.UserService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 功能：修改管理员头像
 * 作者：Luo。
 * 日期：2024/1/29 19:21
 */
@WebServlet("/updateAdminPicture")
public class updateAdminPictureController extends HttpServlet {

    UserService userService = new UserService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if (type != null) {
            String url = request.getParameter("url");
            String id = request.getParameter("id");
            User user = new User();
            user.setImg(url);
            user.setId(Integer.valueOf(id));
            int i = userService.updateAdminPicture(user);
            Result result = new Result();
            result.setCode(i + "");
            if (i > 0){
                result.setMsg("更改成功");
            }else {
                result.setMsg("更改失败");
            }
            String jsonString = JSON.toJSONString(result);
            response.getWriter().write(jsonString);
            response.getWriter().close();
        } else {
            if (ServletFileUpload.isMultipartContent(request)) {
                try {
                    // 获取webapp目录的绝对路径
                    String webAppPath = getServletContext().getRealPath("/");

                    DiskFileItemFactory factory = new DiskFileItemFactory();

                    File uploadDir = new File(webAppPath + File.separator + "imgs");
                    factory.setRepository(uploadDir);

                    ServletFileUpload upload = new ServletFileUpload(factory);

                    List<FileItem> fileItems = upload.parseRequest(request);

                    for (FileItem fileItem : fileItems) {
                        if (!fileItem.isFormField()) {
                            String fileName = new File(fileItem.getName()).getName();
                            String filePath = uploadDir + File.separator + fileName;
                            fileItem.write(new File(filePath));
                        }
                    }

                    response.getWriter().write("File uploaded successfully!");
                } catch (FileUploadException e) {
                    e.printStackTrace();
                    response.getWriter().write("File upload failed.");
                } catch (Exception e) {
                    e.printStackTrace();
                    response.getWriter().write("Exception occurred: " + e.getMessage());
                }
            } else {
                response.getWriter().write("Request does not contain any file.");
            }
        }
    }
}
